package model.components.links.link;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map.Entry;

public class VirtualLink extends Link
{
	private int startingDate;
	public int getStartingDate(){ return startingDate; }
	public void setStartingDate( int startingDate ){ this.startingDate = startingDate; }
	
	private int endingDate;
	public int getEndingDate(){ return endingDate; }
	public void setEndingDate( int endingDate ){ this.endingDate = endingDate; }
	
	private int lifeTime;
	public int getLifeTime(){ return lifeTime; }
	public void setLifeTime( int lifeTime ){ this.lifeTime = lifeTime; }
	
	private LinkedHashMap< SdnLink, BWbackupFlag > sdnLinksBWbackupFlags;
	public LinkedHashMap< SdnLink, BWbackupFlag > getSdnLinksBWbackupFlags(){ return sdnLinksBWbackupFlags; }
	public void setSdnLinksBWbackupFlags( LinkedHashMap< SdnLink, BWbackupFlag > sdnLinksBWbackupFlags ){ this.sdnLinksBWbackupFlags = sdnLinksBWbackupFlags; }

	private int bwPrice;
	public int getBWprice(){ return bwPrice; }
	public void setBWprice( int bwPrice ){ this.bwPrice = bwPrice; }
	
	public VirtualLink( String networkName, int number, int bw )
	{
		super( networkName, VirtualLink.class.getSimpleName(), number, bw, Link.VIRTUAL_LINK );
		
		this.setSdnLinksBWbackupFlags( new LinkedHashMap< SdnLink, BWbackupFlag >() );
	}
	
	public VirtualLink( String networkName, int startingDate, int endingDate, int number, int bw )
	{
		super( networkName, VirtualLink.class.getSimpleName(), number, bw, Link.VIRTUAL_LINK );
		
		this.setStartingDate( startingDate );
		this.setEndingDate( endingDate );
		this.setLifeTime( endingDate - startingDate );
		this.setSdnLinksBWbackupFlags( new LinkedHashMap< SdnLink, BWbackupFlag >() );
	}
	
	@Override
	public VirtualLink getCopy()
	{
		VirtualLink virtualLink = new VirtualLink( this.getNetworkName(), this.getStartingDate(), this.getEndingDate(), this.getNumber(), this.getBW() );
		virtualLink.setNamePrefix( this.getNamePrefix() );
		virtualLink.setName( this.getName() );
		virtualLink.setFirstNode( this.getFirstNode().getCopy() );
		virtualLink.setSecondNode( this.getSecondNode().getCopy() );
		virtualLink.setTimeStamp( this.getTimeStamp() );
		virtualLink.setSelected( this.isSelected() );
		virtualLink.setBWprice( this.getBWprice() );
		
		virtualLink.setSdnLinksBWbackupFlags( new LinkedHashMap< SdnLink, BWbackupFlag >() );
		
		Iterator< Entry< SdnLink, BWbackupFlag > > iterator = this.getSdnLinksBWbackupFlags().entrySet().iterator();
		
		while( iterator.hasNext() )
		{
			Entry< SdnLink, BWbackupFlag > entry = iterator.next();
			virtualLink.getSdnLinksBWbackupFlags().put( entry.getKey().getSoftCopy(), entry.getValue().getCopy() );
		}
				
		return virtualLink;
	}
	
	public void put( SdnLink sdnLink, int bw, int backupFlag )
	{
		this.getSdnLinksBWbackupFlags().put( sdnLink, new BWbackupFlag( bw, backupFlag ) );
	}
	
	public void remove( SdnLink removeSdnLink )
	{
		Iterator< Entry< SdnLink, BWbackupFlag > > sdnLinksBWbackupFlagsIterator = this.getSdnLinksBWbackupFlags().entrySet().iterator();
		SdnLink sdnLink = null;
		
		while( sdnLinksBWbackupFlagsIterator.hasNext() )
		{
			sdnLink = sdnLinksBWbackupFlagsIterator.next().getKey();
			 
			 if( removeSdnLink.equals( sdnLink ) )
				 break;
		}
		
		this.getSdnLinksBWbackupFlags().remove( sdnLink );
	}

}
